package com.dongqb.win;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;

public class OptionDialog {

	private JFrame frame;
	private int mouseX, mouseY;
	public JImagePanel panelRun;
	protected JComboBox<String> refSelect;
	protected JComboBox<String> cnSelect;
	protected JButton saveBtn;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OptionDialog window = new OptionDialog();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public OptionDialog() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setForeground(Color.DARK_GRAY);
		frame.setBackground(Color.DARK_GRAY);
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWin.class.getResource("/com/dongqb/exe/info.png")));
		frame.setBounds(100, 100, 610, 500);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setUndecorated(true); // 取消原生标题栏
		frame.setType(Window.Type.UTILITY); // 关键设置，不显示在任务栏
		frame.setAlwaysOnTop(true);// 总置顶显示

		// 主面板使用BorderLayout
		JPanel mainPanel = new JPanel(new BorderLayout());
		frame.setContentPane(mainPanel);

		// 创建自定义标题栏
		JPanel titleBar = new JPanel(new BorderLayout());
		titleBar.setBackground(Color.BLACK);
		titleBar.setPreferredSize(new Dimension(frame.getWidth(), 30)); // 明确设置高度
		titleBar.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

		// 标题栏左侧图标
		JLabel iconLabel = new JLabel();
		try {
			ImageIcon icon = new ImageIcon(
					Toolkit.getDefaultToolkit().getImage(MainWin.class.getResource("/com/dongqb/exe/option.png")));
			iconLabel.setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)));
		} catch (Exception e) {
			// 加载失败时使用默认文本
			iconLabel.setText("LOGO");
			iconLabel.setForeground(Color.WHITE);
		}
		iconLabel.setOpaque(false);

		// 标题栏右侧关闭按钮
		JButton closeButton = new JButton("×");
		closeButton.setVerticalAlignment(SwingConstants.TOP);
		closeButton.setHorizontalAlignment(SwingConstants.RIGHT);
		closeButton.setFont(new Font("Arial", Font.BOLD, 16));
		closeButton.setForeground(Color.WHITE);
		closeButton.setContentAreaFilled(false);
		closeButton.setBorderPainted(false);
		closeButton.setFocusPainted(false);
		closeButton.addActionListener(e -> frame.setVisible(false));
		// 鼠标悬停效果
		closeButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				closeButton.setForeground(Color.RED);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				closeButton.setForeground(Color.WHITE);
			}
		});

		// 添加拖拽功能
		titleBar.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				mouseX = e.getX();
				mouseY = e.getY();
			}
		});

		titleBar.addMouseMotionListener(new MouseAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				frame.setLocation(e.getXOnScreen() - mouseX, e.getYOnScreen() - mouseY);
			}
		});

		// 将组件添加到标题栏
		JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
		leftPanel.setOpaque(false);
		leftPanel.add(iconLabel);
		titleBar.add(leftPanel, BorderLayout.WEST);

		JLabel lblNewLabel = new JLabel("配置");
		lblNewLabel.setForeground(Color.LIGHT_GRAY);
		leftPanel.add(lblNewLabel);

		JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		rightPanel.setOpaque(false);
		rightPanel.add(closeButton);
		titleBar.add(rightPanel, BorderLayout.EAST);

		// 添加标题栏到主面板
		mainPanel.add(titleBar, BorderLayout.NORTH);

		JPanel contentPanel = new JPanel();
		mainPanel.add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8BBE\u7F6E", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 10, 590, 180);
		contentPanel.add(panel);
		panel.setLayout(null);
		
		JLabel label_1 = new JLabel("列表显示数量：");
		label_1.setBounds(53, 83, 90, 15);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("条");
		label_2.setBounds(276, 83, 54, 15);
		panel.add(label_2);
		
		JLabel label = new JLabel("秒");
		label.setBounds(276, 41, 54, 15);
		panel.add(label);
		
		JLabel lblNewLabel_2 = new JLabel("数据刷新频率：");
		lblNewLabel_2.setBounds(53, 41, 90, 15);
		panel.add(lblNewLabel_2);
		
		saveBtn = new JButton("保存配置");
		saveBtn.setVerticalAlignment(SwingConstants.BOTTOM);
		saveBtn.setBounds(155, 130, 115, 23);
		panel.add(saveBtn);
		
		refSelect = new JComboBox<String>();
		refSelect.setModel(new DefaultComboBoxModel<String>(new String[] {"10", "30", "60", "120", "300"}));
		refSelect.setBounds(155, 38, 115, 21);
		panel.add(refSelect);
		
		cnSelect = new JComboBox<String>();
		cnSelect.setModel(new DefaultComboBoxModel<String>(new String[] {"5", "6", "7", "8", "9", "10"}));
		cnSelect.setBounds(153, 80, 113, 21);
		panel.add(cnSelect);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "\u64CD\u4F5C\u8BF4\u660E", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 200, 590, 260);
		contentPanel.add(panel_1);
		panel_1.setLayout(null);
		
		JTextArea txtradelete = new JTextArea();
		txtradelete.setText("1、本软件仅仅是为了方便看盘功能，免费，开源\r\n2、本软件仅只支持我国大A股股票，其他市场接口数据懒得对接\r\n3、列表上为了简洁好看，没有删除按钮，选中某只股票后，点击“delete”按钮就删掉了\r\n4、数据刷新频率如果不要求那么实时，尽量选择长一些的时间\r\n5、列表显示数量用于控制界面高度的，炒股嘛没必要看太多啦，所以设置了最大10个上限");
		txtradelete.setColumns(10);
		txtradelete.setBackground(Color.WHITE);
		txtradelete.setEditable(false);
		txtradelete.setEnabled(false);
		txtradelete.setLineWrap(true);
		txtradelete.setBounds(10, 22, 570, 228);
		panel_1.add(txtradelete);
	}
	/**
	 * 显示，绑定显示位置
	 */
	public void show(Point point) {

		// 计算右下角坐标
		int x = point.x - frame.getWidth() - 2;
		int y = frame.getHeight()+20;

		// 设置窗口位置
		frame.setLocation(x, y);
		frame.setVisible(true);
	}
	
	/**
	 * 隐藏窗口
	 */
	public void hide() {
		frame.setVisible(false);
		if(panelRun!=null) {
			panelRun.distory();
		}
	}
}
